package com.example.daxiguaforharmony.game.core.base;

import com.example.daxiguaforharmony.game.core.base.DisplayObject;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;

import java.util.ArrayList;

public class DisplayObjectContainer extends DisplayObject {
    public ArrayList<DisplayObject> children = null;

    public DisplayObjectContainer() {
        super();
        this.children = new ArrayList<DisplayObject>();
    }

    public void render(Canvas canvas, Paint paint) {
        super.render(canvas, paint);
        for (int i = 0; i < this.children.size(); i++) {
            DisplayObject child = this.children.get(i);
            if (child == null || child.visible == false) continue;
            child.render(canvas, paint);
        }
    }

    public void addChild(DisplayObject child) {
        if (this.children.indexOf(child) >= 0) return;
        child.setParent(this);
        this.children.add(child);
    }

    public void setChildIndex(DisplayObject child, int index)
    {
        int oldIndex = this.children.indexOf(child);
        if (oldIndex == index) return;
        if (oldIndex == -1) return;
        this.children.remove(oldIndex);
        if (index >= this.children.size())
            this.children.add(child);
        else
            this.children.add(index, child);
    }

    public void removeChild(DisplayObject child) {
        if (this.children.indexOf(child) >= 0) {
            this.children.remove(child);
        }
    }
	
	public void setVisible(boolean visible) {
		for (int i = 0; i < this.children.size(); i++) {
            DisplayObject child = this.children.get(i);
            if (child == null) continue;
            child.visible = visible;
        }
	}
}
